package neusoft.edu.cn.guanshangshiguang.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import neusoft.edu.cn.guanshangshiguang.Listener.ListListener;
import neusoft.edu.cn.guanshangshiguang.Listener.OnItemClickListener;
import neusoft.edu.cn.guanshangshiguang.R;
import neusoft.edu.cn.guanshangshiguang.activities.BorrowDetailsActivity;
import neusoft.edu.cn.guanshangshiguang.activities.LoginActivity;
import neusoft.edu.cn.guanshangshiguang.activities.OnlineBookList_Activity;
import neusoft.edu.cn.guanshangshiguang.adapter.BookTypeListAdapter;
import neusoft.edu.cn.guanshangshiguang.adapter.BorrowAdapter;
import neusoft.edu.cn.guanshangshiguang.bean.BookTypeBean;
import neusoft.edu.cn.guanshangshiguang.bean.BorrowBean;
import neusoft.edu.cn.guanshangshiguang.model.BookTypeModel;
import neusoft.edu.cn.guanshangshiguang.model.BorrowListModel;

public class CollectionFragment extends Fragment {
    private java.util.List<BorrowBean> List;
    RecyclerView recyclerView;
    BorrowAdapter viewAdapter;
    SwipeRefreshLayout swipeRefreshLayout;
    private int flag=0;

    ListListener listListener= new ListListener() {
        @Override
        public void onResponse(List list) {
            List = list;
            viewAdapter.setDate(List);
            if(flag==1){
                flag=0;
                swipeRefreshLayout.setRefreshing(false);
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getContext(), "失败："+msg, Toast.LENGTH_SHORT).show();
        }
    };


    OnItemClickListener onItemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Intent intent = new Intent(getContext(),BorrowDetailsActivity.class);
            intent.putExtra("school_bid",List.get(position).getSchool_bid());
            intent.putExtra("bpic",List.get(position).getBpic());
            intent.putExtra("bname",List.get(position).getBname());
            intent.putExtra("borrow_time",List.get(position).getBorrow_time());
            intent.putExtra("intro",List.get(position).getIntroduction());
            startActivity(intent);
        }
    };

    public CollectionFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_collection, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        RecyclerViewInit(view);

        BorrowListModel bookTypeModel = new BorrowListModel();
        bookTypeModel.getResultList(LoginActivity.UserId,listListener);
    }

    private void RecyclerViewInit(View view) {
        viewAdapter = new BorrowAdapter();

        recyclerView = view.findViewById(R.id.recycler_borrow);

        //每个item如果是确定高度，设置此项提高性能
        recyclerView.setHasFixedSize(true);

        swipeRefreshLayout = view.findViewById(R.id.swipeRefresh);

        swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimary),
                getResources().getColor(R.color.colorAccent),
                getResources().getColor(R.color.colorPrimaryDark));

        swipeRefreshLayout.setProgressViewOffset(true,50,60);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                flag=1;
                BorrowListModel bookTypeModel = new BorrowListModel();
                bookTypeModel.getResultList(LoginActivity.UserId,listListener);

            }
        });

        //这里的第二个参数6代表的是网格的列数
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
        viewAdapter.setOnItemClickListener(onItemClickListener);
        recyclerView.setAdapter(viewAdapter);
    }

}

